package com.kyzh.core.pager.me.user

import android.os.Bundle
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.SafetyV3
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import com.kyzh.core.activities.BaseFragmentActivity
import com.kyzh.core.activities.BindPhoneActivity
import com.kyzh.core.activities.ModifPasswordActivity
import com.kyzh.core.databinding.ActivitySafetyBinding
import com.kyzh.core.databinding.ItemSafetyBinding
import com.kyzh.core.utils.loadImage
import org.jetbrains.anko.*

class SafetyActivity : BaseActivity(),ResultListener {

    private lateinit var binding:ActivitySafetyBinding
    private val beans = arrayListOf<SafetyV3.Nav>()
    private val adapter = Adapter(R.layout.item_safety,beans)
    private var email = " "
    private var phone = " "
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySafetyBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.rvList.adapter =adapter
        binding.tvStatus.setOnClickListener {
            startActivity<ModifPasswordActivity>()
        }
    }

    override fun onResume() {
        super.onResume()
        initView()
    }
    private fun initView() {
        UserRequest.safety {
            binding.let {
                it.tvScore.text = grade.toString()
                it.tvSafe.text = this.dengji
                it.tv3.text = getString(R.string.changePassword)
                it.tv5.text = getString(R.string.loginPassword)
                it.tvStatus.text = getString(R.string.change)
                it.ivIcon.loadImage(nav.last().icon)
                this@SafetyActivity.email = this.email
                this@SafetyActivity.phone = this.phone
                beans.clear()
                beans.addAll(nav)
                beans.removeAt(beans.size-1)
                adapter.notifyDataSetChanged()
            }
        }
    }




    private inner class Adapter(layout:Int,beans:ArrayList<SafetyV3.Nav>):BaseQuickAdapter<SafetyV3.Nav,BaseDataBindingHolder<ItemSafetyBinding>>(layout,beans){
        override fun convert(holder: BaseDataBindingHolder<ItemSafetyBinding>, item: SafetyV3.Nav) {
            holder.dataBinding?.let {
                it.data = item
                it.tvDesc.text = if (item.type=="3"){
                    phone
                }else if (item.type=="4"){
                    email
                }else{
                    ""
                }
                it.tvStatus.setOnClickListener {
                    when(item.type){
                        "0"->{
                            if (item.status==getString(R.string.goVerify)||item.status==getString(R.string.noVerify)){
                                startActivity<VerifiedActivity>()
                            }
                        }
                        "3"->{
                            if (item.status==getString(R.string.change)||item.status==getString(R.string.binded)){
                                startActivity<BaseFragmentActivity>(GlobalKeys.TYPE to BaseFragmentActivity.UNBIND_PHONE)
                            }else{
                                startActivity<BindPhoneActivity>(GlobalKeys.TYPE to GlobalKeys.PHONE)
                            }

                        }
                        "4"->{
                            if (item.status==getString(R.string.change)||item.status==getString(R.string.binded)){
                                startActivity<BaseFragmentActivity>(GlobalKeys.TYPE to BaseFragmentActivity.UNBIND_EMAIL)
                            }else{
                                startActivity<BindPhoneActivity>(GlobalKeys.TYPE to GlobalKeys.EMAIL)
                            }

                        }
                    }
                }
            }
        }
    }
}
